package com.suddeb.gui;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

/*
 * Starting point of the application
 */
public class MainActivity extends Activity {
	private static final String tag = "MainActivity";
	private static final String BILL_TOTAL = "BILL_TOTAL";
	private static final String CUSTOM_PERCENT = "CUSTOM_PERCENT";
	private EditText billEditText, tip10EditText, tip15EditText, tip20EditText;
	private EditText total10EditText, total15EditText, total20EditText;
	private EditText splitBetween;
	private EditText total10EditTextPerPerson, total15EditTextPerPerson, total20EditTextPerPerson;
	private Button calculateButton;
	private SeekBar cusSeekBar;
	private TextView cuTextView;
	private EditText tipCustomText, totalCustomText;
	private EditText totalCustomTextPerPerson;
	private double currentBill;
	private int currentCustomPerecent;
	private int splitBtw;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// Check if the application is restored
		if (savedInstanceState == null) {
			currentBill = 0;
			currentCustomPerecent = 18;
		} else {
			currentBill = savedInstanceState.getDouble(BILL_TOTAL);
			currentCustomPerecent = savedInstanceState.getInt(CUSTOM_PERCENT);
		}

		billEditText = (EditText) findViewById(R.id.billEditText);
		tip10EditText = (EditText) findViewById(R.id.edit10TipText);
		tip15EditText = (EditText) findViewById(R.id.edit15TipText);
		tip20EditText = (EditText) findViewById(R.id.edit20TipText);
		total10EditText = (EditText) findViewById(R.id.edit10TotalText);
		total15EditText = (EditText) findViewById(R.id.edit15TotalText);
		total20EditText = (EditText) findViewById(R.id.edit20TotalText);
		cusSeekBar = (SeekBar) findViewById(R.id.customSeekBar);
		cuTextView = (TextView) findViewById(R.id.textCustomView);
		tipCustomText = (EditText) findViewById(R.id.editCustomTipText);
		totalCustomText = (EditText) findViewById(R.id.editCustomTotalText);
		calculateButton = (Button) findViewById(R.id.calculateButton);
		splitBetween = (EditText) findViewById(R.id.splitBetween);
		total10EditTextPerPerson = (EditText) findViewById(R.id.edit10TotalTextPerPerson);
		total15EditTextPerPerson = (EditText) findViewById(R.id.edit15TotalTextPerPerson);
		total20EditTextPerPerson = (EditText) findViewById(R.id.edit20TotalTextPerPerson);
		totalCustomTextPerPerson = (EditText) findViewById(R.id.editCustomTotalTextPerPerson);
		
		Log.d(tag, "Before addTextChangedListener");

		//Register all Callback Function
		cusSeekBar.setOnSeekBarChangeListener(customSeekBarChangeListener);
		calculateButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				try {
					currentBill = Double.parseDouble(billEditText.getText()
							.toString());
				} catch (NumberFormatException e) {
					e.printStackTrace();
				}
				// Update standard and custom tips
				updateStandard();
				updateCustom();
			}
		});

	}

	//Call Back Function for SeekbarChange
	private OnSeekBarChangeListener customSeekBarChangeListener = new OnSeekBarChangeListener() {

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			currentCustomPerecent = seekBar.getProgress();
			updateCustom();

		}
	};

	// Create Tip and Total Amount for Standard
	private void updateStandard() {
		try{
			splitBtw = Integer.parseInt(splitBetween.getText().toString());
		}catch(NumberFormatException e){
			e.printStackTrace();
		}
		
		if(splitBtw < 1){
			Toast.makeText(getApplicationContext(), "You want to pay all.. :-)" , Toast.LENGTH_SHORT).show();	
			splitBtw = 1;
		}
		
		// Calculate for 10%
		double tenPercentTip = currentBill * 0.1;
		double tenPercentTotal = currentBill + tenPercentTip;
		double tenPercentTotalPerPerson = tenPercentTotal/splitBtw;

		tip10EditText.setText(String.format("%.02f", tenPercentTip));
		total10EditText.setText(String.format("%.02f", tenPercentTotal));
		total10EditTextPerPerson.setText(String.format("%.02f", tenPercentTotalPerPerson));

		// Calculate for 15%
		double fifteenPercentTip = currentBill * 0.15;
		double fifteenPercentTotal = currentBill + fifteenPercentTip;
		double fifteenPercentTotalPerPerson = fifteenPercentTotal/splitBtw;

		tip15EditText.setText(String.format("%.02f", fifteenPercentTip));
		total15EditText.setText(String.format("%.02f", fifteenPercentTotal));
		total15EditTextPerPerson.setText(String.format("%.02f", fifteenPercentTotalPerPerson));

		// Calculate for 20%
		double twentyPercentTip = currentBill * 0.2;
		double twentyPercentTotal = currentBill + twentyPercentTip;
		double twentyPercentTotalPerPerson = twentyPercentTotal/splitBtw;

		tip20EditText.setText(String.format("%.02f", twentyPercentTip));
		total20EditText.setText(String.format("%.02f", twentyPercentTotal));
		total20EditTextPerPerson.setText(String.format("%.02f", twentyPercentTotalPerPerson));
	}

	// Create Tip and Total Amount for Custom
	private void updateCustom() {
		cuTextView.setText(currentCustomPerecent + "%");

		double customTipAmount = currentBill * currentCustomPerecent * 0.01;
		double customTotalAmount = currentBill + customTipAmount;
		double customTotalAmountPerPerson = customTotalAmount/splitBtw;

		tipCustomText.setText(String.format("%.02f", customTipAmount));
		totalCustomText.setText(String.format("%.02f", customTotalAmount));
		totalCustomTextPerPerson.setText(String.format("%.02f", customTotalAmountPerPerson));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onSaveInstanceState(android.os.Bundle)
	 */
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putDouble(BILL_TOTAL, currentBill);
		outState.putInt(CUSTOM_PERCENT, currentCustomPerecent);
	}

}
